Thread: [noob] i can't even name the problem

  1. #16
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Thumbs up The Program Is Solved

    well obviously the code took -300 also as the temperature
    because, it comes under the case
    Code:
    if(temp<cool)
    so, it is better if you use a seperate statement like
    Code:
    if(temp==-300)
    {
              break;
    }
    and make all the other statements else if to this statement
    also put the day++ inside the else if blocks
    also, while initializing the variables,
    you have to put day=0.othrwise an error flags in claculating the error

    for your convinience, sorry to say, i am displaying my code
    Code:
    //done in TurboC
    //please see the logic
    #include <stdio.h>
    #include<conio.h>
    
    void main()
    {
    	int day=0;
    	int cool=0,hot=0,sum=0;
    	int counter,temp,x,y;
    	float avg;
    
    	clrscr();
    	while (temp!=-300)
    	{
    		printf("Enter the temperature for day %d (-300 to end): ");
    		scanf("%d",&temp);
    
    		++counter;
    		if(temp==-300)
    		{
    			break;
    		}
    
    		else if (temp>hot)
    		{
    			day++;
    			hot=temp;   
    			x=day;       
    			sum+=temp;
    		}
    
    		else if (temp<cool)
    		{
    			day++;
    			cool=temp;      
    			y=day;          
    			sum+=temp;
    		}
    
    
    	}
    
    	avg=sum/day;
    
    	printf("The hottest day is day %d and the temperature is %d degrees Celcius.\n",x,hot);
    	printf("The coolest day is day %d and the temperature is %d degrees Celcius.\n",y,cool);
    	printf("The average temperature over %d day(s) is %f degrees Celcius.\n",day-1,avg);
    
    	getch();
    }

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > but miracle c gives a stupid "C:\Program Files\Miracle C\self\fusun.c: line 17:
    Miracle C is the biggest piece of crap on the net - it is NOT a C compiler, it is someone trying to flog their compiler construction homework off as a compiler. It isn't, not by a long way in any shape or form a credible C compiler. Even 20+ years old Turbo-C spanks this lousy excuse for a compiler.

    The other point is to not develop your programs in the same directory structure where your compiler is installed. Create a "MyCode" in your own personal area.

    You yourself a huge favour and get dev-c++ (www.bloodshed.net) and see what a real compiler will do for you. What's more, it is really FREE, not that nagware P-O-S called miracle-c.

    > void main()
    Nope - try again. Everyone else was getting it right in this thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    you realize this would only count the days that are the hottest and coolest , give their average, and say that it is the average for (number of hottest days + number of coldest days -1) days?
    coolshyam
    Code:
    void main() 
    {
    	int day=0;
    	int cool=0,hot=0,sum=0;
    	int counter,temp,x,y;
    	float avg;
    
    	clrscr();
    	while (temp!=-300)
    	{
    		printf("Enter the temperature for day %d (-300 to end): ");
    		scanf("%d",&temp);
    
    		++counter;// this should be initialized and used, or omitted
    		if(temp==-300)
    		{
    			break;
    		}
    
    		else if (temp>hot)
    		{
    			day++;
    			hot=temp;   
    			x=day;       
    			sum+=temp;
    		}
    
    		else if (temp<cool)
    		{
    			day++;
    			cool=temp;      
    			y=day;          
    			sum+=temp;
    		}
    
    
    	}
    
    	avg=sum/day;// this isn't going to be a float
    	printf("The hottest day is day %d and the temperature is %d degrees Celcius.\n",x,hot);
    	printf("The coolest day is day %d and the temperature is %d degrees Celcius.\n",y,cool);
    	printf("The average temperature over %d day(s) is %f degrees Celcius.\n",day-1,avg);
    
    	getch();
    }
    The problem is that the variable day doesn't keep track of the total number of days, only days that are hotter than the hottest or cooler than the coolest. Then you use that number to compute the average of the temperatures that were hotter than the hottest or cooler than the coolest. Also, 0 probably isn't the best initial value for 'cool' since you aren't guarranteed to have a freezing day in your sample set. Lastly, when you divide an int by an int, the result is an int, not a float. You can cast it as a float like in one of the previous posts and do it that way if you want.

    So, fixing those things, you might end up with something like
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int cool=100, counter=0, hot=0, sum=0, temp=0, x=0, y=0;
            float avg=0;
            while (1)
            {
                    ++counter;
                    printf("Enter the temperature for day %d (-300 to end): ", counter);
                    scanf("%d",&temp);
                    if(temp==-300)
                    {
                            --counter;
                            break;
                    }
    
                    else if (temp>hot)
                    {
                            hot=temp;
                            x=counter;
                    }
    
                    else if (temp<cool)
                    {
                            cool=temp;
                            y=counter;
                    }
                    sum+=temp;
            }
            if(counter)
            {
                  avg=(float)sum/counter;
            }
            printf("The hottest day is day %d and the temperature is %d degrees Celcius.\n",x,hot);
            printf("The coolest day is day %d and the temperature is %d degrees Celcius.\n",y,cool);
            printf("The average temperature over %d day(s) is %f degrees Celcius.\n",counter, avg);
    }
    straight off the heap

  4. #19
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    Your last code entered would compile and generally work if you changed the test to ( temp <= -274.0 ). I don't think you need to worry about any valid entries below absolute zero. The conflict was between int and float variables.

    On the other hand if you only want to enter int values, call temp an int - you can add it to a float total without conversion ( total += temp; ) the compiler will convert it for you from int to float and add it in, but it won't compare unlike variable types without you casting them
    to be the same. You can also divide a float by an int ( for total/daycount ).

  5. #20
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    thanks everyone. great community =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM